import uuid, traceback

from flask import Blueprint, request, jsonify, send_file
from service.nw_szy.nw_szy_service import get_all_data, get_data_by_uuid, add_data, update_data, delete_data, download_excel, check_szy_ready
from config import ini_op

# app = Flask(__name__)
# 创建 Blueprint
nw_szy_blueprint = Blueprint('nw_szy', __name__)


# 获取所有记录
@nw_szy_blueprint.route('/api/data', methods=['GET'])
def get_data():
    try:
        data = get_all_data()
        return jsonify(data), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 获取单条记录
@nw_szy_blueprint.route('/api/data/<string:uuid>', methods=['GET'])
def get_single_data(uuid):
    try:
        data = get_data_by_uuid(uuid)
        if data:
            return jsonify(data), 200
        else:
            return jsonify({"message": "Data not found"}), 404
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 添加记录
@nw_szy_blueprint.route('/api/data', methods=['POST'])
def add_new_data():
    try:
        data = request.get_json()
        dwbh = data.get('dwbh')
        model = data.get('model')
        xhbh = data.get('xhbh')
        xhmc = data.get('xhmc')
        fybh = data.get('fybh')
        fymc = data.get('fymc')
        je = data.get('je')

        add_data(uuid.uuid4(), dwbh, model, xhbh, xhmc, fybh, fymc, je)
        return jsonify({"message": "Data added successfully!"}), 201
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 更新记录
@nw_szy_blueprint.route('/api/data/<string:uuid>', methods=['PUT'])
def update_existing_data(uuid):
    try:
        data = request.get_json()
        dwbh = data.get('dwbh')
        model = data.get('model')
        xhbh = data.get('xhbh')
        xhmc = data.get('xhmc')
        fybh = data.get('fybh')
        fymc = data.get('fymc')
        je = data.get('je')

        update_data( uuid, dwbh, model, xhbh, xhmc, fybh, fymc, je )
        return jsonify({"message": "Data updated successfully!"}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500


# 删除记录
@nw_szy_blueprint.route('/api/data/<string:uuid>', methods=['DELETE'])
def delete_existing_data(uuid):
    try:
        delete_data(uuid)
        return jsonify({"message": "Data deleted successfully!"}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@nw_szy_blueprint.route('/api/download', methods=['POST'])
def download_data_controller():
    try:
        data = request.json
        p_dwbh = data.get('dwbh')
        p_model = data.get('model')
        p_qzq_type = data.get('qzq_type')
        need_report_desc = data.get('need_report_desc')

        ini_op.opinivalue('basic_config', need_report_desc=need_report_desc)
        excel_filename, excel_path = download_excel(p_dwbh, p_model, p_qzq_type)
        return send_file(excel_path, as_attachment=True, download_name=excel_filename, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    except Exception as e:
        print(traceback.format_exc())
        # 处理可能的错误
        return jsonify({'error': str(e)}), 500

@nw_szy_blueprint.route('/api/check_szy_ready', methods=['POST'])
def check_szy_ready_controller():
    try:
        data = request.json
        p_dwbh = data.get('dwbh')
        p_model = data.get('model')
        p_qzq_type = data.get('qzq_type')
        result = check_szy_ready(p_dwbh, p_model, p_qzq_type)
        return jsonify({'code': 200, 'data': result}), 200
    except Exception as e:
        # 处理可能的错误
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    # 在应用启动时创建表
    from service.nw_szy.nw_szy_service import create_table

    create_table()
    # app.run(debug=True)
